package cn.quevo.springmvc.spring.boot.controller;

import cn.quevo.core.entity.ResultEntity;
import cn.quevo.core.enums.FileTypeEnum;
import cn.quevo.core.lang.EnumUtils;
import cn.quevo.core.lang.StringUtils;
import cn.quevo.spring.SpringController;
import cn.quevo.spring.entity.Upload;
import cn.quevo.spring.util.FileUploadUtils;
import cn.quevo.springmvc.spring.boot.autoconfigure.UploadProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.nio.file.Paths;

/**
 * 文件上传
 *
 * @author July july_sky@foxmail.com
 * @version 1.0
 * @date 2018/07/09 18:23:11
 * @Copyright 东方浩星（北京）传媒科技有限公司版权所有.All Rights Reserved.
 */
@EnableConfigurationProperties(UploadProperties.class)
@RestController("uploadController")
@RequestMapping("/fupload/")
public class UploadController extends SpringController {

    @Resource(name = "uploadProperties")
    private UploadProperties uploadProperties;

    @RequestMapping("uploadImage")
    public ResultEntity uploadImage(HttpServletRequest request) {
        Upload upload = new Upload();
        upload.setCopyPath(uploadProperties.getCopy());
        upload.setFileTypeEnum(FileTypeEnum.IMG);
        upload.setDiskPath(getRoot(uploadProperties.getDisk(), request.getParameter("upload")));
        upload.setSavePath(getRoot(uploadProperties.getRoot(), request.getParameter("upload")));
        if (!StringUtils.isEmpty(uploadProperties.getFileExts())) {
            upload.setExts(uploadProperties.getFileExts().get(FileTypeEnum.IMG.getCode().toLowerCase()));
        }
        if (!StringUtils.isEmpty(uploadProperties.getFileSize())) {
            upload.setMaxSize(uploadProperties.getFileSize().get(FileTypeEnum.IMG.getCode().toLowerCase()));
        }
        return FileUploadUtils.upload(upload, request);
    }

    @RequestMapping("uploadFile")
    public ResultEntity uploadFile(HttpServletRequest request) {
        Upload upload = new Upload();
        upload.setCopyPath(uploadProperties.getCopy());
        upload.setFileTypeEnum(formatFileType(request.getParameter("dirType")));
        upload.setDiskPath(uploadProperties.getDisk());
        upload.setSavePath(getRoot(uploadProperties.getRoot(), request.getParameter("upload")));
        if (!StringUtils.isEmpty(uploadProperties.getFileExts())) {
            upload.setExts(uploadProperties.getFileExts().get(upload.getFileTypeEnum().getCode().toLowerCase()));
        }
        if (!StringUtils.isEmpty(uploadProperties.getFileSize())) {
            upload.setMaxSize(uploadProperties.getFileSize().get(upload.getFileTypeEnum().getCode().toLowerCase()));
        }
        return FileUploadUtils.upload(upload, request);
    }

    /**
     * 获取相对路径
     *
     * @param root
     * @param upload
     * @return
     */
    private String getRoot(String root, String upload) {
        root = StringUtils.isBlank(root) ? "" : root;
        upload = StringUtils.isBlank(upload) ? "" : upload;
        return Paths.get(root, upload).normalize().toString();
    }

    /**
     * 获取文件类型枚举
     *
     * @param dirType
     * @return
     */
    private FileTypeEnum formatFileType(String dirType) {
        if (StringUtils.isBlank(dirType) || EnumUtils.existsEnumIgnoreCase(dirType, FileTypeEnum.values())) {
            return FileTypeEnum.CUSTOM;
        }
        return EnumUtils.getEnumIgnoreCase(dirType, FileTypeEnum.values());
    }
}
